package com.example.flight.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.flight.common.JwtUtils;
import com.example.flight.common.Result;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import com.example.flight.entity.AdminEntity;
import com.example.flight.service.AdminService;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author hws
 * @email 231884142@qq.com
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody AdminEntity admin){
        LambdaQueryWrapper<AdminEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminEntity::getUsername,admin.getUsername());
        AdminEntity adminInDB = adminService.getOne(queryWrapper);
        if( null==adminInDB){
            return Result.error().message("无此用户");
        }

        String password = admin.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!adminInDB.getPassword().equals(password)){
            return Result.error().message("密码错误");
        }
        String token = JwtUtils.generateToken("管理用户", adminInDB.getAid());

        return Result.ok().data("token",token);

    }
    /**
     * 信息
     */
    @GetMapping("/{aid}")
    public Result info(@PathVariable("aid") String aid){
		AdminEntity admin = adminService.getById(aid);

        return Result.ok().data("admin", admin);
    }
    /**
     * 登录状态
     */
    @GetMapping()
    public Result loginInfo(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring("Bearer ".length());
        }
        Claims claims = JwtUtils.getClaimsByToken(token);
        AdminEntity admin = adminService.getById((String) claims.get("id"));
        return Result.ok().data("admin", admin);
    }

    /**
     * 保存
     */
/*    @PostMapping("/save")
    public Result save(@RequestBody AdminEntity admin){
        String password = admin.getPassword();
        admin.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
		adminService.save(admin);

        return Result.ok();
    }*/

    /**
     * 修改
     */
  /*  @PutMapping
    public Result update(@RequestBody AdminEntity admin){
        String password = admin.getPassword();
        admin.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        adminService.updateById(admin);

        return Result.ok();
    }*/

    /**
     * 删除
     */
 /*   @DeleteMapping("/{aid}")
    public Result delete(@PathVariable String aid){
        adminService.removeById(aid);

        return Result.ok();
    }*/

}
